package com.example.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.TradeInfo;
import com.example.entity.vo.TradeDetailVO;
import com.example.mapper.TradeDetailMapper;
import com.example.service.TradeInfoService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 交易信息管理控制器
 */
@Controller
@RequestMapping("/admin/trade")
public class TradeInfoController {

    @Autowired
    private TradeInfoService tradeInfoService;
    
    @Autowired
    private TradeDetailMapper tradeDetailMapper;

    /**
     * 交易信息管理页面
     */
    @GetMapping("/page")
    public String tradePage() {
        return "admin/trade/index";
    }

    /**
     * 获取交易信息列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<Page<TradeDetailVO>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "type", required = false) String type) {
        
        Page<TradeDetailVO> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<TradeDetailVO> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like(TradeDetailVO::getCardId, keyword)
                    .or()
                    .like(TradeDetailVO::getCustomerName, keyword);
        }
        
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.eq(TradeDetailVO::getTradeType, type);
        }
        
        queryWrapper.orderByDesc(TradeDetailVO::getTradeDate);
        
        Page<TradeDetailVO> pageResult = new Page<>();
        pageResult.setRecords(tradeDetailMapper.selectPage(pageParam, queryWrapper).getRecords());
        pageResult.setTotal(tradeDetailMapper.selectCount(queryWrapper));
        pageResult.setCurrent(page);
        pageResult.setSize(limit);
        
        return Result.success(pageResult);
    }

    /**
     * 删除交易记录
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result<?> delete(@PathVariable("id") Integer id) {
        boolean removed = tradeInfoService.removeById(id);
        if (removed) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 导出交易数据
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("交易信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        
        List<TradeDetailVO> list = tradeDetailMapper.selectList(null);
        EasyExcel.write(response.getOutputStream(), TradeDetailVO.class).sheet("交易信息").doWrite(list);
    }
} 